package com.geeyuu.android.model;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.geeyuu.android.GeePrefrence;
import com.geeyuu.android.activity.MainTabActivity;
import com.geeyuu.android.databases.DataBaseHelper;
import com.geeyuu.android.utils.CommonUtil;
import com.geeyuu.android.utils.DBUtil;
import com.geeyuu.android.utils.LogUtil;


public class BaseUserInfo {
    
    private static final String TAG = "UserBaseInfo";
    
	public long uid;	// uint32
	/**
	 * 是否是访客
	 */
	public int isGuest = USER_NOT_GUEST;
	/**
	 * 昵称
	 */
	public String nickName;
	/**
	 * 主头像ID
	 */
	public String mainPhotoID = "";
	/**
	 * 用户角色
	 */
	public int role = ROLE_NONE;	// uint8
	/**
	 * 是否在线
	 */
	public int isOnline = USER_NOT_ONLINE;
	/**
	 * 经度和纬度
	 */
	public String lonAndlat;
	/**
	 * 鲜花数(人气值)
	 */
	public long popValue;		// uint32
	/**
	 * 最后一次在线时间
	 */
	public long lastOnlineTime; // uint32
	/**
	 * 最后一次用户信息修改时间
	 */
	public long lastModifyTime; // uint32
	/**
	 * 相册最后一次更新时间
	 */
	public long lastAlbumModifyTime;
	/**
	 * 公开相册ID(逗号隔开)
	 */
	public String publicPhotoIds;
	/**
	 * 私照相册ID(逗号隔开)
	 */
    public String privatePhotoIds;
	/**
	 * 距离
	 */
    public String distance = "";
    /**
     * 年龄
     */
    public int age;
    /**
     * 身高
     */
    public int height;
    /**
     * 体重
     */
    public int weight;
    /**
     * 签名
     */
    public String sign;
    
	public static final int USER_NOT_GUEST = 0;
	public static final int USER_GUEST = 1;
	
	public static final int USER_NOT_ONLINE = 0;
	public static final int USER_ONLINE = 1;
	
	public static final int ROLE_NONE = 0;
	public static final int ROLE_0 = 1;
    public static final int ROLE_1 = 2;
    public static final int ROLE_05 = 3;
    
    public String getRealRole(){
        switch (role) {
        case ROLE_0:
            return "0";
        case ROLE_1:
            return "1";
        case ROLE_05:
            return "0.5";
        }
        return "保密";
    }
    
    public static final String EXTRA_UID = "uid";
    public static final String EXTRA_IS_GUEST = "is_guest";
    public static final String EXTRA_NICK_NAME = "nick_name";
    public static final String EXTRA_MAIN_PHOTO_ID = "main_photo_id";
    public static final String EXTRA_ROLE = "role";
    public static final String EXTRA_IS_ONLINE = "is_online";
    public static final String EXTRA_LON_AND_LAT = "lon_and_lat";
    public static final String EXTRA_POP_VALUE = "pop_value";
    
    public static final String EXTRA_LAST_ONLINE_TIME = "last_online_time";
    public static final String EXTRA_LAST_MODIFY_TIME = "last_modify_time";
    public static final String EXTRA_LAST_ALBUM_MODIFY_TIME = "last_album_modify_time";
    
    public static final String EXTRA_PUBLIC_PHOTO_IDS = "public_photo_ids";
    public static final String EXTRA_PRIVATE_PHOTO_IDS = "private_photo_ids";
    public static final String EXTRA_DISTANCE = "distance";
    
    public static final String EXTRA_AGE = "age";
    public static final String EXTRA_HEIGHT = "height";
    public static final String EXTRA_WEIGHT = "weight";
    public static final String EXTRA_SIGN = "sign";
    
    public String visitTimes;
    public int money;
    public String visitorIds;
    public String accountName;
    
    public static final String EXTRA_VISIT_TIMES = "visit_times";
    public static final String EXTRA_MONEY = "money";
    public static final String EXTRA_VISITOR_IDS = "visitor_ids";
    public static final String EXTRA_ACCOUNT_NAME = "account_name";
    
    public long interestModifyTime;
    public long privateAlbumModifyTime;
    public long shareModifyTime;
    public int myFansCount;
    public long blackModifyTime;
    
    public static final String EXTRA_ATTENTION_MODIFY_TIME = "attention_modify_time";
    public static final String EXTRA_PRIVATE_ALBUM_MODIFY_TIME = "private_album_modify_time";
    public static final String EXTRA_SHARE_MODIFY_TIME = "share_modify_time";
    public static final String EXTRA_MY_FANS_COUNT = "my_fans_count";
    public static final String EXTRA_BLACK_MODIFY_TIME = "black_modify_time";
    
    public static final String[] SELECTION_AROUND_COLUMN = {EXTRA_UID, EXTRA_MAIN_PHOTO_ID, EXTRA_DISTANCE, EXTRA_IS_ONLINE, EXTRA_LON_AND_LAT};
    
    public static final String TABLE_NAME = "user_info";
    
    public static synchronized boolean isUserInMyShareList(Context context, long uid){
        String shareList = GeePrefrence.getShareList(context);
        if(!CommonUtil.isEmpty(shareList)){
            String[] shareListArray = shareList.split(",");
            if(null != shareListArray && shareListArray.length > 0){
                final String uidStr = String.valueOf(uid);
                for(String str : shareListArray){
                    if(str.equals(uidStr)){
                        return true;
                    }
                }
            }
        }
        return false;
    }
    
    public synchronized static void saveOrUpdateUserInfo(Context context, ContentValues cv){
        long uid = cv.getAsLong(EXTRA_UID);
        if(uid > 0){
            if(DataBaseHelper.isUserExist(context, uid)){ //用户已经存在,那就更新
                updateUserInfo(context, cv);
            }else{ //用户不存在，插入
                if(!DataBaseHelper.saveUserInfo(context, cv)){
                    LogUtil.e(TAG, "插入用户信息到数据库失败");
                }else{
                    LogUtil.v(TAG, "插入用户信息到数据库");
                }
            }
        }else{
            LogUtil.e(TAG, "插入用户信息没有uid");
        }
    }
    
    public synchronized static void updateUserInfo(Context context, ContentValues cv){
        long uid = cv.getAsLong(EXTRA_UID);
        if(uid > 0){
            if(!DataBaseHelper.updateUserInfo(context, cv, uid)){
                LogUtil.e(TAG, "更新用户信息到数据库失败");
            }else{
                LogUtil.v(TAG, "更新用户信息到数据库");
            }
        }else{
            LogUtil.e(TAG, "更新用户信息没有uid");
        }
    }
    
    public synchronized static void saveUserInfo(Context context, ContentValues cv) {
        long uid = cv.getAsLong(EXTRA_UID);
        if (uid > 0) {
            if (!DataBaseHelper.saveUserInfo(context, cv)) {
                LogUtil.e(TAG, "插入用户信息到数据库失败");
            } else {
                LogUtil.v(TAG, "插入用户信息到数据库");
            }
        } else {
            LogUtil.e(TAG, "插入用户信息没有uid");
        }
    }
    
    public synchronized static void saveOrUpdateBatch(Context context, ContentValues... cvs){
        if(cvs != null && cvs.length > 0){
            if(DataBaseHelper.saveOrUpdateUserBatch(context, cvs)){
                LogUtil.v(TAG, "批量插入用户信息到数据库");
            }else{
                LogUtil.e(TAG, "批量插入用户信息到数据库失败");
            }
        }else{
            LogUtil.e(TAG, "批量插入参数异常");
        }
    }
    
    public static final int ORDER_BY_DISTANCE = 0;
    public static final int ORDER_BY_POP_VALUE = 1;
    
    public static final int ORDER_BY_NOT_SET = 100;
    public static final int FILTER_ROLE_NOT_SET = 100;
    public static final int FILTER_JUST_ONLINE_NOT_SET = 100;
    
    public synchronized static ArrayList<BaseUserInfo> getAroundUser(Context context, int limit, int orderby, int role, int justOnline){
        Cursor c = DataBaseHelper.getAroundUserCursor(context, limit, orderby, role, justOnline);
        ArrayList<BaseUserInfo> baseUserInfos = new ArrayList<BaseUserInfo>();
        if(null != c){
            BaseUserInfo baseUserInfo;
            while(c.moveToNext()){
                baseUserInfo = new BaseUserInfo();
                baseUserInfo.mainPhotoID = DBUtil.getString(c, BaseUserInfo.EXTRA_MAIN_PHOTO_ID);
                baseUserInfo.uid = DBUtil.getInt(c, BaseUserInfo.EXTRA_UID);
                baseUserInfo.distance = DBUtil.getString(c, BaseUserInfo.EXTRA_DISTANCE);
                baseUserInfo.isOnline = DBUtil.getInt(c, BaseUserInfo.EXTRA_IS_ONLINE);
                baseUserInfo.lonAndlat = DBUtil.getString(c, BaseUserInfo.EXTRA_LON_AND_LAT);
                baseUserInfos.add(baseUserInfo);
            }
            c.close();
            if(!MainTabActivity.isAppShowing){
                DataBaseHelper.close();
            }
        }
        return baseUserInfos;
    }
    
    public synchronized static BaseUserInfo getUserInfo(Context context, long uid){
        BaseUserInfo userInfo = DataBaseHelper.getUserInfo(context, uid);
        if(userInfo == null){
            LogUtil.w(TAG, "查询某个用户信息失败");
        }
        return userInfo;
    }
    
    
    
    public synchronized static String getUserInfoSingleStrColumn(Context context, long uid, String extraColumn) {
        String str = DataBaseHelper.getUserInfoSingleColumn(context, uid, extraColumn, DataBaseHelper.DATA_TYPE_STRING);
        if(CommonUtil.isEmpty(str)){
            LogUtil.v(TAG, "查询某个人的某个字段的数据为空, column name:" + extraColumn + " uid:" + uid);
        }
        return str;
    }

    public synchronized static int getUserInfoSingleIntColumn(Context context, long uid, String extraColumn) {
        String str = DataBaseHelper.getUserInfoSingleColumn(context, uid, extraColumn, DataBaseHelper.DATA_TYPE_INT);
        if(CommonUtil.isEmpty(str)){
            LogUtil.v(TAG, "查询某个人的某个字段的数据为空, column name:" + extraColumn + " uid:" + uid);
            return 0;
        }
        return Integer.valueOf(str);
    }

    public synchronized static long getUserInfoSingleLongColumn(Context context, long uid, String extraColumn) {
        String str = DataBaseHelper.getUserInfoSingleColumn(context, uid, extraColumn, DataBaseHelper.DATA_TYPE_LONG);
        if(CommonUtil.isEmpty(str)){
            LogUtil.v(TAG, "查询某个人的某个字段的数据为空, column name:" + extraColumn + " uid:" + uid);
            return 0l;
        }
        return Long.valueOf(str);
    }
	
}
